﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Core.Domain.Entities;
using Core.Domain.Enums;

namespace DataXml.CommunityFiles
{
  /// <summary>
  /// Representation of an XML file
  /// </summary>
  /// <example>
  /// 
  ///  <Community Name="Sample Community" xsi:noNamespaceSchemaLocation="community.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  ///  <Person LName="Accorti" VotingEligibility="Eligible" FName="Pónt" AKAName="Paul" AgeGroup="Pre-Youth" Voted="No"></Person>
  /// 
  /// </example>
  [XmlRoot("Community")]
  public class XmlCommunity
  {
    [XmlElement(ElementName = "Person")]
    public List<XmlPerson> XmlPeople { get; set; }

    [XmlIgnore]
    public IList<Person> People
    {
      get
      {
        var newList = new List<Person>();
        foreach (var xmlPerson in XmlPeople)
        {
          newList.Add(xmlPerson.AsPerson());
        }
        return newList;
      }
      set { throw new NotImplementedException(); }
    }

    [XmlAttribute(AttributeName = "Name")]
    public string Name { get; set; }

    public void CopyIntoNewStructure(Community community, Election election)
    {
      foreach (var xmlPerson in XmlPeople)
      {
        var person = xmlPerson.AsPerson();
       
        community.Add(person);

        election.Voters.Add(new Voter
                              {
                                    PersonVoting = person,
                                    BallotSubmissionMethod = ConvertVoted(xmlPerson.Voted),
                                    Election = election
                              });
      }
    }

    private BallotSubmissionMethod ConvertVoted(XmlPerson.XmlVoted voted)
    {
      switch (voted)
      {
        case XmlPerson.XmlVoted.No:
          return BallotSubmissionMethod.NotSubmitted;

        case XmlPerson.XmlVoted.VotedInPerson:
          return BallotSubmissionMethod.VotedInPerson;

        case XmlPerson.XmlVoted.DroppedOff:
          return BallotSubmissionMethod.DroppedOffByOther;

        case XmlPerson.XmlVoted.Mailed:
          return BallotSubmissionMethod.MailedIn;

        default:
          return BallotSubmissionMethod.NotSubmitted;
      }
    }
  }
}